package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.Menu;
import com.lagou.domain.ResponseResult;
import com.lagou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单列表查询
     *
     * @return
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu(Integer currentPage,Integer pageSize) {

        PageInfo<Menu> pageInfo = menuService.findAllMenu(currentPage,pageSize);
        return new ResponseResult(true, 200, "菜单列表查询", pageInfo);
    }

    /**
     * 查询菜单信息(回显)
     *
     * @param id 菜单id
     * @return
     */
    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(Integer id) {

        String msg = null;
        Map<String, Object> map = new HashMap<>();
        if (id == -1) {

            map.put("menuInfo", null);
            msg = "添加菜单回显";
        } else {

            Menu menu = menuService.findMenuById(id);
            map.put("menuInfo", menu);
            msg = "修改菜单回显";
        }

        List<Menu> list = menuService.findSubMenuListByPid(-1);
        map.put("parentMenuList", list);
        return new ResponseResult(true, 200, msg, map);
    }

    /**
     * 添加&修改菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("/saveOrUpdateMenu")
    public ResponseResult saveOrUpdateMenu(@RequestBody Menu menu) {

        String msg;
        if (menu.getId() == null) {

            menuService.saveMenu(menu);
            msg = "添加菜单";
        } else {

            menuService.updateMenu(menu);
            msg = "修改菜单";
        }

        return new ResponseResult(true, 200, msg, null);
    }
}
